<?php
require_once ('Controller/basicmodule.inc.php');
require_once ('Model/CircleInfo.inc.php');
require_once ('Model/MobileInfo.inc.php');
require_once ('Model/CircleModel.inc.php');
require_once ('Model/DeviceModel.inc.php');
require_once ('Model/DeviceToCircleModel.inc.php');
class common extends basicmodule {
	public $MobileInfo = NULL;
	public $DeviceModel;
	public $CircleModel;
	public $DeviceToCircleModel;
/***************************************/
#	$DeviceModel
	public function __construct () {
		parent::__construct();
#		$this->mysql = $mysql;
		$this->DeviceModel = new DeviceModel ($this->mysql);
		$this->CircleModel = new CircleModel ($this->mysql);
		$this->DeviceToCircleModel = new DeviceToCircleModel ($this->mysql);
	}


	public function CheckDeviceInformation () {
		$MobileInfo = new MobileInfo ();
		$CheckCount = 0;
/*
$_REQUEST['IMEI'] = '359959040574737';
$_REQUEST['PhoneNumber'] = '123456';
$_REQUEST['SimSerialNumber'] = '8985212110113856';
$_REQUEST['IMSI'] = '454120050856944';
$_REQUEST['Manufacturer'] = 'samsung';
$_REQUEST['Model'] = 'GT-I9100';
$_REQUEST['OS'] = 'android';
$_REQUEST['Version'] = '4.0.4';
*/
		if (array_key_exists ('IMEI', $_REQUEST) && $_REQUEST['IMEI'] != '') {
			$MobileInfo->IMEI = $_REQUEST['IMEI'];
			$CheckCount++; //1
		}
		if (array_key_exists ('PhoneNumber', $_REQUEST) && $_REQUEST['PhoneNumber'] != '') {
			$MobileInfo->PhoneNumber = $_REQUEST['PhoneNumber'];
		}
		if (array_key_exists ('SimSerialNumber', $_REQUEST) && $_REQUEST['SimSerialNumber'] != '') {
			$MobileInfo->SimSerialNumber = $_REQUEST['SimSerialNumber'];
#			$CheckCount++; //2
		}
		if (array_key_exists ('IMSI', $_REQUEST) && $_REQUEST['IMSI'] != '') {
			$MobileInfo->IMSI = $_REQUEST['IMSI'];
#			$CheckCount++; //3
		}
		if (array_key_exists ('Manufacturer', $_REQUEST) && $_REQUEST['Manufacturer'] != '') {
			$MobileInfo->Manufacturer = $_REQUEST['Manufacturer'];
			$CheckCount++; //4
		}
		if (array_key_exists ('Model', $_REQUEST) && $_REQUEST['Model'] != '') {
			$MobileInfo->Model = $_REQUEST['Model'];
			$CheckCount++; //5
		}
		if (array_key_exists ('OS', $_REQUEST) && $_REQUEST['OS'] != '') {
			$MobileInfo->OS = $_REQUEST['OS'];
			$CheckCount++; //6
		}
		if (array_key_exists ('Version', $_REQUEST) && $_REQUEST['Version'] != '') {
			$MobileInfo->Version = $_REQUEST['Version'];
			$CheckCount++; //7
		}
		if (array_key_exists ('lang', $_REQUEST) && $_REQUEST['lang'] != '') {
			$MobileInfo->lang = $_REQUEST['lang'];
		}
		//if ($CheckCount == 7) {
		if ($CheckCount == 5) {
			$Deviceinfo = $this->DeviceModel->CheckDeviceItemExistByIMEI ($MobileInfo->IMEI);
			$MobileInfo->DeviceID = $Deviceinfo[$this->DeviceModel->idRowName];
			if ($MobileInfo->DeviceID != NULL) {
				foreach ($this->DeviceToCircleModel->GetCirclesByDevice ($MobileInfo->DeviceID) as $Circles) {
					$CircleInfo = $this->CircleModel->CheckCircleItemExist ($Circles[$this->DeviceToCircleModel->circleIDRowName]);
					if ($CircleInfo != NULL) {
						$MobileInfo->AddCircle ($Circles[$this->DeviceToCircleModel->circleIDRowName], $CircleInfo[$this->CircleModel->nameRowName], $Circles[$this->DeviceToCircleModel->startDateTimeRowName], $Circles[$this->DeviceToCircleModel->endDateTimeRowName], $CircleInfo[$this->CircleModel->typeRowName]);
					}
				}
			}
			$this->MobileInfo = $MobileInfo;
#			var_dump ($MobileInfo);
		}
	}
/***************************************/

}

/*
class MobileInfo {
	public $IMEI = NULL;
	public $PhoneNumber = NULL;
	public $SimSerialNumber = NULL;
	public $IMSI = NULL;
	public $Manufacturer = NULL;
	public $Model = NULL;
	public $OS = NULL;
	public $Version = NULL;
	public $DeviceID = NULL;
	public $Circles = NULL;
	public $lang = NULL;
	public function AddCircle ($id, $name, $startDate, $endDate) {
		$CircleInfo = new CircleInfo ();
		$CircleInfo->ID = $id;
		$CircleInfo->Name = $name;
		$CircleInfo->StartDate = $startDate;
		$CircleInfo->EndDate = $endDate;
		$this->Circles[] = $CircleInfo;
	}
}

class CircleInfo {
	public $ID = NULL;
	public $Name = NULL;
	public $StartDate = NULL;
	public $EndDate = NULL;
}
*/
?>
